package ua.vn.ticket_scanner.utils;

import android.util.Log;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by Ivan on 21.06.13.
 */
public class Utils {

    private static final String TAG = "Utils";

    public static String md5(String value) {
        if (value == null)
            return null;

        byte[] hash = null;
        try {
            hash = MessageDigest.getInstance("MD5").digest(value.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            Log.e("Huh", "MD5 should be supported?");
        } catch (UnsupportedEncodingException e) {
            Log.e("Huh", "UTF-8 should be supported?");
        }

        if (hash == null) {
            Log.e(TAG, "hash isn't create!");
            return null;
        }

        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10)
                hex.append("0");
            hex.append(Integer.toHexString(b & 0xFF));
        }
        return hex.toString();
    }
}
